﻿using GetData;
using GetData.webProductClass;
using GetDataOpen;
using jutehui.GetData;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace jutehui.GetDataOpen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// "http://www.meidebi.com/guonei_n/" 1 精品,http://www.meidebi.com/haitao_n/2 海淘
        /// </summary>
        GetData.MeiDeBi _mdbClass = new GetData.MeiDeBi();
        jutehui.GetData.SMZDMFX smzdmfx = new GetData.SMZDMFX();
        HotProductsCalss hotProduct = new HotProductsCalss();
        yifen yifenWang = new yifen();
        SMZDM smzde = new SMZDM();
        ZhiZhiZhi zhizhzhi = new ZhiZhiZhi();
        czfxh chaozhi = new czfxh();
        walatao walaTao = new walatao();
        haitao55 haitao = new haitao55();
        tsdxb tianshang = new tsdxb();
        private bool isf = true;
        //System.Timers.Timer timer = new System.Timers.Timer(1000 * 60);
        private int _startHot = 0;
        private bool isRun = true;
        Thread th = null;
        Thread upth = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            //定义风云榜商品参数
            //timer.Elapsed += new System.Timers.ElapsedEventHandler(SaveHotProducts);
            //timer.AutoReset = true;//一直执行
            //timer.Enabled = true;
            //timer.Start();
            //th = new Thread(StopThread);
            //th.Start();
            //upth = new Thread(UpdateDBData);
            //upth.Start();
            //th = new Thread(Savemgpyh);
            //th.Start();
            //th = new Thread(ss);
            //th.Start(); 
            //th = new Thread(Savezhizhizhi);
            //th.Start();

            //Thread th1 = new Thread(ss);
            //th1.Start();
            //Thread.Sleep(2000);
            //Thread th2 = new Thread(ShihuoSave);
            //th2.Start();
            //Thread.Sleep(2000);
            //Thread th3 = new Thread(huihui);
            //th3.Start();
            //Thread.Sleep(2000);
            //Thread th4 = new Thread(liuzhuni);
            //th4.Start();
            //Thread.Sleep(2000);
            //Thread th5 = new Thread(juchaozhi);
            //th5.Start();
            //Thread.Sleep(2000);
            //Thread th6 = new Thread(SaveSMZDM);
            //th6.Start();
            //Thread.Sleep(2000);
            //Thread th7 = new Thread(Savemgpyh);
            //th7.Start();
            //Thread.Sleep(2000);
            //Thread th8 = new Thread(SaveSMZDMJX);
            //th8.Start();
            //Thread.Sleep(2000);
            //Thread th9 = new Thread(Savezhizhizhi);
            //th9.Start();
            //Thread.Sleep(2000);
            //Thread th10 = new Thread(Saveczfxh);
            //th10.Start();
            //Thread.Sleep(2000);
            //Thread th11 = new Thread(Saveyifenw);
            //th11.Start();
            //Thread.Sleep(2000);
            //Thread th12 = new Thread(SaveWaLaTao);
            //th12.Start();
            //Thread th13 = new Thread(Savetsdxb);
            //th13.Start();

            //timer1.Interval = 1000 * 60 * 90;
            //timer1.Start();
            //timer2.Interval = 1000 * 10;
            //timer2.Start();
            //timer3.Interval = 1000 * 60;
            //timer3.Start();
        }
        /// <summary>
        /// 自动定期更新数据库
        /// </summary>
        public void UpdateDBData(object o)
        {
            while (isf)
            {
                DateTime dtime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                try
                {
                    if (dtime.Hour == 1)
                    {
                        CustomMethod.UpdateData();
                    }
                }
                catch (Exception e)
                {
                }
                Thread.Sleep(1000 * 60 * 60);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(ss);
            th.Start(null);
        }
        /// <summary>
        /// 没得比
        /// </summary>
        public void ss(object o)
        { 
            while (isf)
            {
                if (isRun)
                {
                    string msg = "";
                    //抓取精品信息http://m.meidebi.com/guonei-new.html
                    _mdbClass.GetProductsToDataBase("http://www.meidebi.com/guonei_n/", (int)NavigateType.精选, ref msg);
                    if (msg != "")
                    {
                        LogOperate.WriteLog(msg);
                        msg = "";
                    }

                    ////http://m.meidebi.com/haitao-new.html
                    //_mdbClass.GetProductsToDataBase("http://www.meidebi.com/haitao_n/", (int)NavigateType.海淘, ref msg);
                    //if (msg != "")
                    //{
                    //    LogOperate.WriteLog(msg);
                    //    msg = "";
                    //}60000

                }
                Random rm = new Random(); 
                //3  到10分钟 遍历一次
                int timr = rm.Next(180000, 600000);
                Thread.Sleep(timr);
            }
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //string aa = webBrowser1.Url.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(SaveSMZDM);
            th.Start(null);
        }

        /// <summary>
        /// 什么值得买
        /// </summary>
        public void SaveSMZDM(object o)
        {
            SMZDMFX zhidemai = new SMZDMFX();
            while (isf)
            {
                if (isRun)
                {
                    zhidemai.GetSaleURL("https://faxian.smzdm.com/h1s0t0f0c3p1/#filter-block", (int)NavigateType.精选);

                //    zhidemai.GetSaleURL("http://haitao.smzdm.com/", (int)NavigateType.海淘);

                }
                Thread.Sleep(1000 * 60 * 3);
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }
        public enum NavigateType
        {
            精选 = 1,
            海淘 = 2,
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(ShihuoSave);
            th.Start(null);
        }
        /// <summary>
        /// 识货网
        /// </summary> 
        public void ShihuoSave(object o)
        {
            ShiHuo shihuo = new ShiHuo();
            while (isf)
            {
                if (isRun)
                {
                    shihuo.GetProductsToDataBase("http://www.shihuo.cn/youhui#qk=daohang&order=2", (int)NavigateType.精选);

                   // shihuo.GetProductsToDataBase("http://www.shihuo.cn/youhui/list?type=2#qk=shaixuan", (int)NavigateType.海淘);

                }
                Thread.Sleep(1000 * 60*2);
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Savemgpyh);
            th.Start(); 
        }
        /// <summary>
        /// 买个便宜货
        /// </summary>
        public void Savemgpyh()
        {
            mgpyhClass mgpyh = new mgpyhClass();
            while (isf)
            {
                if (isRun)
                {
                    mgpyh.GetProductsToDataBase("http://www.mgpyh.com/");

                }
                Thread.Sleep(1000 * 60 * 3);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //isRun = true;
            Thread th = new Thread(huihui);
            th.Start(null);
        }
        /// <summary>
        /// 惠惠网
        /// </summary>
        public void huihui(object o)
        {
            HuiHuiWang huihuiwang = new HuiHuiWang();
            while (isf)
            {
                if (isRun)
                {
                    huihuiwang.GetProductsToDataBase("http://www.huihui.cn/m/editor");

                }
                Thread.Sleep(1000 * 60 * 3);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {

            Thread th = new Thread(liuzhuni);
            th.Start(null);
        }
        /// <summary>
        /// 留住你
        /// </summary>
        public void liuzhuni(object o)
        {
            liuzhuni liuZhuni = new liuzhuni();
            while (isf)
            {
                if (isRun)
                {
                    liuZhuni.GetProductsToDataBase("http://www.huim.com/");
                }
                Thread.Sleep(1000 * 60 * 2);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(juchaozhi);
            th.Start(null);
        }
        /// <summary>
        /// 今日聚超值
        /// </summary>
        public void juchaozhi(object o)
        {
            jinrijuchaozhi jinri = new jinrijuchaozhi();
            while (isf)
            {
                if (isRun)
                {
                    jinri.GetProductsToDataBase("http://best.pconline.com.cn/");

                }
                Thread.Sleep(1000 * 60 * 2);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
        }

        public void SaveHotProducts(object source, System.Timers.ElapsedEventArgs e)
        {

            int minit = DateTime.Now.Minute;

            if (isRun)
            {
                if ((minit >= 0 && minit < 4) || _startHot == 0)
                {
                    _startHot = 1;
                    hotProduct.GetProductsToDataBase("http://guangdiu.com/rank.php?");//http://guangdiu.com/rank.php");
                }

            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(SaveSMZDMJX);
            th.Start(null);
        }
        /// <summary>
        /// 什么值得买发现
        /// </summary>
        public void SaveSMZDMJX(object o)
        {
            while (isf)
            {
                if (isRun)
                {
                    smzde.GetSaleURL("http://www.smzdm.com/");

                }
                Thread.Sleep(1000 * 60 * 2);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Savezhizhizhi);
            th.Start(null);

        }
        /// <summary>
        /// 值值值
        /// </summary>
        public void Savezhizhizhi(object o)
        {
            while (isf)
            {
                if (isRun)
                {
                    zhizhzhi.GetSaleURL("http://zhizhizhi.com/gn/", (int)NavigateType.精选);

                  ///  zhizhzhi.GetSaleURL("http://zhizhizhi.com/haitao/", (int)NavigateType.海淘);

                }
                Thread.Sleep(1000 * 60 * 5);
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Saveczfxh);
            th.Start();

        }
        /// <summary>
        /// 超值分享汇
        /// </summary>
        public void Saveczfxh(object o)
        {
            while (isf)
            {
                if (isRun)
                {
                    chaozhi.GetSaleURL("http://www.czfxh.com/", (int)NavigateType.精选);

                  //  chaozhi.GetSaleURL("http://www.czfxh.com/category/hai_wai_jing_pin", (int)NavigateType.海淘);

                }
                Thread.Sleep(1000 * 60 * 3);
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Saveyifenw);
            th.Start(null);
        }
        /// <summary>
        /// 一分网
        /// </summary>
        public void Saveyifenw(object o)
        {
            while (isf)
            {
                if (isRun)
                {
                    yifenWang.GetSaleURL("http://www.yifen.com/deals/", (int)NavigateType.精选);
                    yifenWang.GetSaleURL("http://www.yifen.com/haitao/", (int)NavigateType.海淘);

                }
                Thread.Sleep(1000 * 60 * 3);
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(SaveWaLaTao);
            th.Start(null);
        }
        /// <summary>
        /// 瓦拉淘
        /// </summary>
        public void SaveWaLaTao(object o)
        {
            while (isf)
            {
                if (isRun)
                {
                    walaTao.GetSaleURL("http://www.walatao.com/");
                }
                Thread.Sleep(1000 * 60 * 2);
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Save55haitao);
            th.Start(null);
        }
        public void Save55haitao(object o)
        {
            while (isf)
            {
                if (isRun)
                {
                    haitao.GetSaleURL("http://www.55haitao.com/");
                }
                Thread.Sleep(1000 * 60 * 2);
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Savetsdxb);
            th.Start(null);
        }
        /// <summary>
        /// 天上掉馅饼
        /// </summary>
        public void Savetsdxb(object o)
        {
            while (isf)
            {
                if (isRun)
                {
                    tianshang.GetSaleURL("http://www.tsdxb.com/15-0-1", (int)NavigateType.海淘);
                    tianshang.GetSaleURL("http://www.tsdxb.com/", (int)NavigateType.精选);
                }
                Thread.Sleep(1000 * 60 * 3);
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            //    isRun = true;
            //    //没得比
            //    //th = new Thread(ss);
            //    //th.Start();
            //    //什么值得买 精选
            //    th = new Thread(SaveSMZDM);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //识货网
            //    th = new Thread(ShihuoSave);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //买个便宜货
            //    th = new Thread(Savemgpyh);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //惠惠网
            //    th = new Thread(huihui);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //留住你
            //    th = new Thread(liuzhuni);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //今日聚超值
            //    th = new Thread(juchaozhi);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //什么值得买发现
            //    th = new Thread(SaveSMZDMJX);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //值值值
            //    th = new Thread(Savezhizhizhi);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //超值分享汇
            //    th = new Thread(Saveczfxh);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //一分网
            //    th = new Thread(Saveyifenw);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //瓦拉淘
            //    th = new Thread(SaveWaLaTao);
            //    th.Start();
            //    Thread.Sleep(30000);
            //    //天上掉馅饼
            //    th = new Thread(Savetsdxb);
            //    th.Start();

            //    Thread.Sleep(30000);
            th = new Thread(StopThread);
            th.Start();
            Thread.Sleep(30000);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            isRun = false;

        }
        /// <summary>
        /// 线程控制 抓取数据程序停止和开始
        /// </summary>
        private void StopThread(object o)
        {
            while (isf)
            {
                bool isGoToFunction = false;
                string dateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                int dtime = Convert.ToDateTime(dateTimeStr).Hour;
                if (dtime <= 23 && dtime >= 6)
                {
                    isRun = true;

                }
                else
                {
                    isRun = false;
                }
                ////isGoToFunction true时  说明程序在工作时间段  否则在非工作时间段
                //if (isGoToFunction)
                //{ 
                //    isRun = true; 
                //}
                //else
                //{
                //    isRun = false;
                //}
                Thread.Sleep(60 * 1000 * 30);
            }

        }

        private void button18_Click(object sender, EventArgs e)
        {
            isf = false;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            CustomMethod.UpdateData();
        }

        private void button20_Click(object sender, EventArgs e)
        {
            //Thread th = new Thread(SavetTeMai);
            //th.Start();
        }
        /// <summary>
        /// 淘宝九块九
        /// </summary>
        //public void SavetTeMai()
        //{
        //    while (isf)
        //    {
        //        if (isRun)
        //        {
        //            //for (int index = 107; index >= 101; index--)
        //            //{
        //            tmallJiukuaijiu jiukuaijiu = new tmallJiukuaijiu();
        //            string url = "http://temai.taobao.com/cheap.htm?cid=101";// +index;//?spm=a21ap.7701290.2001.2.UmluBZ&extra=&cid=
        //            //url += index;
        //            jiukuaijiu.GetSaleURL(url, (Enum.Parse(typeof(TmallType), "101")).ToString());
        //            Thread.Sleep(1000 * 60);
        //            // }

        //        }
        //        Thread.Sleep(1000 * 60 * 13 * 60);
        //    }
        //}

        /// <summary>
        /// 淘宝特卖网页的类别id
        /// </summary>
        public enum TmallType
        {
            女装 = 101,
            内衣 = 102,
            鞋包配饰 = 103,
            母婴 = 104,
            居家 = 105,
            美食 = 106,
            美妆 = 107,
            男装 = 108,
            数码家电 = 109,
            运动户外 = 110
        }

        private void button21_Click(object sender, EventArgs e)
        {
            Process.Start(Process.GetCurrentProcess().ProcessName + ".exe");
            Environment.Exit(0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Process.Start(Process.GetCurrentProcess().ProcessName + ".exe");
            Environment.Exit(0);
        }

        private void button22_Click(object sender, EventArgs e)
        {
            GC.Collect();
        }
        /// <summary>
        /// 线程池创建线程开启抓取数据工作
        /// </summary>
        public void StartThreads()
        {
            try
            {
                ThreadPool.SetMaxThreads(15, 17);
                ThreadPool.QueueUserWorkItem(new WaitCallback(ss));

                ThreadPool.QueueUserWorkItem(new WaitCallback(ShihuoSave));

                ThreadPool.QueueUserWorkItem(new WaitCallback(huihui));

                ThreadPool.QueueUserWorkItem(new WaitCallback(liuzhuni));

                ThreadPool.QueueUserWorkItem(new WaitCallback(juchaozhi));

                ThreadPool.QueueUserWorkItem(new WaitCallback(SaveSMZDM));

                ThreadPool.QueueUserWorkItem(new WaitCallback(Saveczfxh));
                ThreadPool.QueueUserWorkItem(new WaitCallback(Savezhizhizhi));

                ThreadPool.QueueUserWorkItem(new WaitCallback(SaveSMZDMJX));

                ThreadPool.QueueUserWorkItem(new WaitCallback(Saveyifenw));

                ThreadPool.QueueUserWorkItem(new WaitCallback(SaveWaLaTao));

                ThreadPool.QueueUserWorkItem(new WaitCallback(Savetsdxb));
                ThreadPool.QueueUserWorkItem(new WaitCallback(StopThread));
                ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateDBData));

            }
            catch (Exception ex)
            {

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            // StartThreads();
            //Thread th1 = new Thread(ss);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(ShihuoSave);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(huihui);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(liuzhuni);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(juchaozhi);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(SaveSMZDM);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(Savemgpyh);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(SaveSMZDMJX);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(Savezhizhizhi);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(Saveczfxh);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(Saveyifenw);
            //th1.Start();
            //Thread.Sleep(2000);
            //th1 = new Thread(SaveWaLaTao);
            //th1.Start();
            //th1 = new Thread(Savetsdxb);
            //th1.Start();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            int minit = DateTime.Now.Minute;

            if (isRun)
            {
                if ((minit >= 0 && minit < 4) || _startHot == 0)
                {
                    _startHot = 1;
                    hotProduct.GetProductsToDataBase("http://guangdiu.com/rank.php?");//http://guangdiu.com/rank.php");
                }

            }
        }
    }
}
